class Game
{
    constructor(el)
    {
        this.el = document.querySelector(el)
        //创造Map
        this.map = new Map(this.el)
        this.map.create()
        //创造Food
        this.food = new Food (this.el)
        this.food.create()
        //创造snake
        this.snake = new Snake(this.el)
        this.snake.create()
        //定时器
        this.t1 = null
    }

    start()
    {
        clearInterval(this.t1)
        this.t1 = setInterval(() => {
            this.snake.move()
            console.log(this.t1);
            let snakeX = this.snake.snakeObj[0].x * 20
            let snakeY = this.snake.snakeObj[0].y * 20
            let foodX = this.food.foodX
            let foodY = this.food.foodY
            if (snakeX == foodX && snakeY == foodY) {
                let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]
                this.snake.snakeObj.push({ 
                    x: last.x, 
                    y :last.x, 
                    color:last.color, 
                    ele:null
                })
                this.snake.create()
                this.food.foodMove()
            }

        },150)
        document.addEventListener('keydown',(e) => {
            e = e || window.event

            switch (e.keyCode) {
                case 38:
                    this.snake.direction = 'top'
                    break;
                case 40:
                    this.snake.direction = 'bottom'
                    break;
                case 37:
                    this.snake.direction = 'left'
                    break;
                case 39:
                    this.snake.direction = 'right'
                    break;
                default:
                    break;
            }
        })
    }

    stop() 
    {
        clearInterval(this.t1);
        console.log(11111);
    }
}

